package com.itheima.dubbo.web;


import com.itheima.dubbo.UserService;
import com.itheima.dubbo.pojo.User;
import org.apache.dubbo.common.URL;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.apache.dubbo.rpc.RpcContext;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/user")
public class UserController {

   // @DubboReference(timeout = 1000,retries = 3) //引入实体
   // @DubboReference(check = false)
    @DubboReference(retries = 2,cluster = "failfast")
    UserService userService;

    //配置相同，消费方设置时间优先级高于提供方
    @GetMapping("/hello")
     public String hello(String username){
        return userService.hello(username);
     }

     @GetMapping("/find")
     public User find(String username){
         return userService.findUser(username);
     }
    @GetMapping("findOne/{id}")
    public User findOne(@PathVariable("id") Long id) {

/*        // 本端是否为消费端，这里会返回true
  boolean isConsumerSide = RpcContext.getContext().isConsumerSide();
        // 获取最后一次调用的提供方IP地址
    String serverIP = RpcContext.getContext().getRemoteHost();*/
        // 获取当前服务配置信息，所有配置信息都将转换为URL的参数
     String application = RpcContext.getContext().getUrl().getParameter("application");
     URL url = RpcContext.getContext().getUrl();

        // 隐式传参
        RpcContext.getContext().setAttachment("company", "itheima");

        return userService.findOne(id);
    }
}